package com.youyi.yyscreencutlibrary.Core;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;

import com.lmiot.xyclick.Util.LkxLog;
import com.youyi.yyscreencutlibrary.SDK.YYScreenCutSDK;
import java.lang.ref.SoftReference;
import java.nio.ByteBuffer;

public class YYScreenShotHelper {
    Handler backgroundHandler;
    private final Intent mData;
    private ImageReader mImageReader;
    private boolean mIsFirstCut;
    private MediaProjection mMediaProjection;
    private OnScreenShotListener mOnScreenShotListener;
    private final SoftReference<Context> mRefContext;
    private final int mResultCode;
    private VirtualDisplay mVirtualDisplay;

    public interface OnScreenShotListener {
        void onFinish(Bitmap bitmap);
    }

    public YYScreenShotHelper(Context context, int i, Intent intent, OnScreenShotListener onScreenShotListener) {
        this.mOnScreenShotListener = onScreenShotListener;
        this.mRefContext = new SoftReference<>(context);
        this.mResultCode = i;
        this.mData = intent;
        startCut(i, intent);
        YYScreenCutSDK.getInstance().setOnScreenCutDestroyListener(new YYScreenCutSDK.OnScreenCutDestroyListener() {
            @Override // com.youyi.yyscreencutlibrary.SDK.YYScreenCutSDK.OnScreenCutDestroyListener
            public void result(boolean z) {
                YYScreenShotHelper.this.destroy(null);
            }
        });
    }

    private void startCut(int i, Intent intent) {
        try {
            MediaProjection mediaProjection = getMediaProjectionManager().getMediaProjection(i, intent);
            this.mMediaProjection = mediaProjection;
            mediaProjection.registerCallback(new MediaProjection.Callback() {
                /* class com.youyi.yyscreencutlibrary.Core.YYScreenShotHelper.AnonymousClass2 */

                public void onStop() {
                    super.onStop();
                }
            }, null);
//            this.mImageReader = ImageReader.newInstance(getScreenWidth(), getScreenHeight(), 2, 1);
//            if (YYScreenCutSDK.getYunPhoneFlag(getContext())) {
//                this.mImageReader = ImageReader.newInstance(getScreenWidth(), getScreenHeight(), 2, 1);
//            } else {
                this.mImageReader = ImageReader.newInstance(getScreenWidth(), getScreenHeight(), 1, 1);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler getBackgroundHandler() {
        if (this.backgroundHandler == null) {
            HandlerThread handlerThread = new HandlerThread("catwindow", 10);
            handlerThread.start();
            this.backgroundHandler = new Handler(handlerThread.getLooper());
        }
        return this.backgroundHandler;
    }

    public void startScreenShot() {
        try {
            this.mVirtualDisplay = this.mMediaProjection.createVirtualDisplay("screen-mirror", getScreenWidth(), getScreenHeight(), Resources.getSystem().getDisplayMetrics().densityDpi, 16, this.mImageReader.getSurface(), null, null);
            this.mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
                public void onImageAvailable(final ImageReader imageReader) {
                    LkxLog.d("startScreenShot onImageAvailable:"+imageReader);
                    if (YYScreenShotHelper.this.mOnScreenShotListener != null) {
                        if (!YYScreenShotHelper.this.mIsFirstCut) {
                            YYScreenShotHelper.this.mIsFirstCut = true;
                            if (Build.VERSION.SDK_INT >= 29) {
//                                try {
                                    Image acquireLatestImage = imageReader.acquireLatestImage();
                                    if (acquireLatestImage != null) {
                                        acquireLatestImage.close();
                                    }
//                                } catch (Exception e) {
//                                    e.printStackTrace();
//                                }
                            }
                        }
                        new Handler().postDelayed(new Runnable() {
                            public void run() {
                                Image image;
                                OnScreenShotListener onScreenShot = null;
                                try{
                                    if ((image = mImageReader.acquireLatestImage()) != null && image.getPlanes().length > 0) {
                                        Bitmap uBitmap = getScreenBitmap(image);
                                        if (mOnScreenShotListener != null) {
                                            mOnScreenShotListener.onFinish(uBitmap);
                                        }
                                        if (image != null) {
                                            image.close();
                                        }
                                        if (Build.VERSION.SDK_INT < 29 && mMediaProjection != null) {
                                            mMediaProjection.stop();
                                        }
                                    }
                                    return;
                                }catch(java.lang.Exception e2){
                                    Exception uException = e2;
                                }
                            }
                        }, 500);
                        return;
                    }
                    return;
                }
            }, getBackgroundHandler());
        } catch (Exception e) {
            e.printStackTrace();
            YYScreenCutSDK.setYunPhoneFlag(getContext(), !YYScreenCutSDK.getYunPhoneFlag(getContext()));
        }
    }

    private void destroy(Image image) {
        if (image != null) {
            try {
                image.close();
            } catch (Exception e) {
                e.printStackTrace();
                if (this.mImageReader != null) {
                    this.mImageReader = null;
                }
                if (this.mVirtualDisplay != null) {
                    this.mVirtualDisplay = null;
                }
                if (this.mMediaProjection == null) {
                    return;
                }
            } catch (Throwable th) {
                if (this.mImageReader != null) {
                    this.mImageReader = null;
                }
                if (this.mVirtualDisplay != null) {
                    this.mVirtualDisplay = null;
                }
                if (this.mMediaProjection != null) {
                    this.mMediaProjection = null;
                }
                throw th;
            }
        }
        ImageReader imageReader = this.mImageReader;
        if (imageReader != null) {
            imageReader.close();
        }
        MediaProjection mediaProjection = this.mMediaProjection;
        if (mediaProjection != null) {
            mediaProjection.stop();
        }
        VirtualDisplay virtualDisplay = this.mVirtualDisplay;
        if (virtualDisplay != null) {
            virtualDisplay.release();
        }
        ImageReader imageReader2 = this.mImageReader;
        if (imageReader2 != null) {
            imageReader2.setOnImageAvailableListener(null, null);
        }
        MediaProjection mediaProjection2 = this.mMediaProjection;
        if (mediaProjection2 != null) {
            mediaProjection2.stop();
        }
        if (this.mImageReader != null) {
            this.mImageReader = null;
        }
        if (this.mVirtualDisplay != null) {
            this.mVirtualDisplay = null;
        }
        if (this.mMediaProjection == null) {
            return;
        }
        this.mMediaProjection = null;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private Bitmap getScreenBitmap(Image image) {
        int width = image.getWidth();
        int height = image.getHeight();
        Image.Plane[] planes = image.getPlanes();
        ByteBuffer buffer = planes[0].getBuffer();
        int pixelStride = planes[0].getPixelStride();
        Bitmap createBitmap = Bitmap.createBitmap(((planes[0].getRowStride() - (pixelStride * width)) / pixelStride) + width, height, Bitmap.Config.ARGB_8888);
        createBitmap.copyPixelsFromBuffer(buffer);
        Bitmap createBitmap2 = Bitmap.createBitmap(createBitmap, 0, 0, width, height);
        image.close();
        if (isScreenOriatationPortrait(getContext())) {
            int width2 = createBitmap2.getWidth() * createBitmap2.getHeight();
            int[] iArr = new int[width2];
            createBitmap2.getPixels(iArr, 0, createBitmap2.getWidth(), 0, 0, createBitmap2.getWidth(), 1);
            int i = 0;
            while (true) {
                if (i >= width2) {
                    i = 0;
                    break;
                } else if (iArr[i] != 0) {
                    break;
                } else {
                    i++;
                }
            }
            try {
                return Bitmap.createBitmap(createBitmap2, i, 0, width - (i * 2), height);
            } catch (Exception e) {
                e.printStackTrace();
                return createBitmap2;
            }
        } else {
            int width3 = createBitmap2.getWidth() * createBitmap2.getHeight();
            int[] iArr2 = new int[width3];
            createBitmap2.getPixels(iArr2, 0, 1, 0, 0, 1, createBitmap2.getHeight());
            int i2 = 0;
            while (true) {
                if (i2 >= width3) {
                    i2 = 0;
                    break;
                } else if (iArr2[i2] != 0) {
                    break;
                } else {
                    i2++;
                }
            }
            int i3 = height - (i2 * 2);
            try {
                createBitmap2 = Bitmap.createBitmap(createBitmap2, 0, i2, width, i3);
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            return YYScreenCutSDK.getInstance().isAllScreenDevice(getContext()) ? Bitmap.createBitmap(createBitmap2, YYScreenCutSDK.StarBarHeight, 0, width - YYScreenCutSDK.StarBarHeight, i3) : createBitmap2;
        }
    }

    private MediaProjectionManager getMediaProjectionManager() {
        return (MediaProjectionManager) getContext().getSystemService("media_projection");
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private Context getContext() {
        return this.mRefContext.get();
    }

    public static int getScreenWidth() {
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight() {
        return Resources.getSystem().getDisplayMetrics().heightPixels;
    }

    public boolean isScreenOriatationPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == 1;
    }
}
